/**
 * web2010_admin
 * cn.aresoft.logic.service.implement.system.role RoleServiceImpl.java
 */
package com.xsoft.logic.service.implement.sys.role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.xsoft.framework.service.BaseService;
import com.xsoft.logic.model.sys.Module;
import com.xsoft.logic.model.sys.ModuleRef;
import com.xsoft.logic.model.sys.UserRolePrivilege;
import com.xsoft.logic.service.interfaces.sys.role.RoleService;


/**
 * @author 潘冬 2010-8-9 下午01:26:28
 */
public class RoleServiceImpl extends BaseService implements RoleService {
	
	private String deleteUserRole;
	private String insertUserRole;
	private String selectAllModule;
	private String selectRoleModuleRef;
	private String getUserRolePrivilege;
	private String deleteRolePrivilege;
	
	public String getDeleteRolePrivilege() {
		return deleteRolePrivilege;
	}


	public void setDeleteRolePrivilege(String deleteRolePrivilege) {
		this.deleteRolePrivilege = deleteRolePrivilege;
	}


	public String getSelectRoleModuleRef() {
		return selectRoleModuleRef;
	}


	public void setSelectRoleModuleRef(String selectRoleModuleRef) {
		this.selectRoleModuleRef = selectRoleModuleRef;
	}


	public String getGetUserRolePrivilege() {
		return getUserRolePrivilege;
	}


	public void setGetUserRolePrivilege(String getUserRolePrivilege) {
		this.getUserRolePrivilege = getUserRolePrivilege;
	}


	public String getDeleteUserRole() {
		return deleteUserRole;
	}


	public void setDeleteUserRole(String deleteUserRole) {
		this.deleteUserRole = deleteUserRole;
	}


	public String getInsertUserRole() {
		return insertUserRole;
	}


	public void setInsertUserRole(String insertUserRole) {
		this.insertUserRole = insertUserRole;
	}


	@Override
	public String getTableName() {
		// TODO Auto-generated method stub
		return "Role";
	}


	@Override
	public List<Module> selectAllModule() {
		return (List<Module>)dao.findForList("selectAllModule", null);
	}


	@Override
	public boolean setRoleFunction(Long roleId, ModuleRef... functions) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public List<ModuleRef> viewRoleLimits(Long roleId) {
		List<ModuleRef> moduleRefList = dao.findForList(selectRoleModuleRef, roleId);
		return moduleRefList;
	}


	@Override
	public void updateUserRole(Long userId, String roleIds) {
		dao.remove("deleteUserRole", userId);
		if(roleIds.length()>0){
			String[] rIdArray = roleIds.split(",");
			HashMap<String,Long> map = new HashMap<String,Long>();
			map.put("userId", userId);
			for(String s : rIdArray){
				map.put("roleId", Long.parseLong(s));
				dao.insert("insertUserRole", map);
			}
		}
	}


	public String getSelectAllModule() {
		return selectAllModule;
	}


	public void setSelectAllModule(String selectAllModule) {
		this.selectAllModule = selectAllModule;
	}


	@Override
	public void updatePrivilege(Long Id, String type, String menuIds) {
		HashMap delMap = new HashMap();
		delMap.put("id", Id);
		delMap.put("type", type);
		dao.remove("deletePrivilege", delMap);
		
		if(menuIds.length()>0){
			String[] menuIdArray = menuIds.split(",");
			HashMap	map = new HashMap();
			map.put("id", Id);
			map.put("type", type);
			for(String s : menuIdArray){
				map.put("menuId", Long.parseLong(s));
				dao.insert("insertPrivilege", map);
			}
		}
	}


	@Override
	public ArrayList<UserRolePrivilege> getUserRolePrivilege(HashMap map) {
		// TODO Auto-generated method stub
		return (ArrayList<UserRolePrivilege>)dao.findForList(getUserRolePrivilege, map);
	}


	@Override
	public void deleteRolePrivilege(Long roleId) {
		dao.remove(deleteRolePrivilege, roleId);
	}


}
